public class Java1003 {

    public int val;
    public Java1003 next;
    public Java1003 head;


    public Java1003(int val) {
        this.val = val;
    }

    public void creatList() {

        Java1003 node1 = new Java1003(12);
        Java1003 node2 = new Java1003(23);
        Java1003 node3 = new Java1003(34);
        Java1003 node4 = new Java1003(45);
        Java1003 node5 = new Java1003(56);


        node1.next = node2;  //将前一个结点的地址赋予给上一个结点的.next字段
        node2.next = node3;  //从而建立链接
        node3.next = node4;
        node4.next = node5;

        this.head = node1;

    }

    //计算长度
    public int size(){
        Java1003 x = head;
        int count = 0;
        while (x != null){
            count++;
            x = x.next;
        }

        return count;
    }

    //判断是否包含某个值
    public boolean contain(int key){
        Java1003 x = head;

        while (x != null){
            if(x.val == key){    //注意是x.val，x代替head往前走
                return true;
            }
            x = x.next;
        }
        return false;
    }

    public void addFirst(int key){
        Java1003 node = new Java1003(key);

        node.next = head;      //先绑定后面的值，再确定头
        head = node;
    }

    public void addLast(int key){
        Java1003 node = new Java1003(key);
        if(size() == 0)
        {
            head = node;
            return;
        }

        Java1003 x = head;
        while (x.next != null){
            x = x.next;
        }
            x.next = node;
    }

//    public int searchIndex(int index){
//        int len = size();
//        int count = 0;
//
//        if(index < 0 || index > len){
//            return 0;
//        }
//
//        if(index == 0){
//            return 0;
//        }
//
//        Java1003 x = head;
//        while (index - 1 != 0){
//            x = x.next;
//            count++;
//        }
//
//        return count;
//    }

    public void show() {
        Java1003 x = head;

        while (x != null){
            System.out.println(x.val);
            x = x.next;
        }
    }


}

